package com.jiage.managerlibrary;

import android.app.Activity;

import java.util.Iterator;
import java.util.Stack;

public class SDActivityManager {
    private static Stack<Activity> mStackActivity;
    private static SDActivityManager mInstance;

    private SDActivityManager() {
        mStackActivity = new Stack<Activity>();
    }

    public static SDActivityManager getInstance() {
        if (mInstance == null) {
            synchronized (SDActivityManager.class) {
                if (mInstance == null) {
                    mInstance = new SDActivityManager();
                }
            }
        }
        return mInstance;
    }
    public void onCreate(Activity activity) {
        addActivity(activity);
    }

    public void onResume(Activity activity) {
        addActivity(activity);
    }

    public void onDestroy(Activity activity) {
        removeActivity(activity);
    }

    /**
     * 添加
     * @param activity
     */
    private void addActivity(Activity activity) {
        if (!mStackActivity.contains(activity)) {
            mStackActivity.add(activity);
        }
    }

    /**
     * 结束
     * @param activity
     */
    private void removeActivity(Activity activity) {
        if (activity != null) {
            mStackActivity.remove(activity);
        }
    }

    /**
     * 获取最后一个activity
     * @return
     */
    public Activity getLastActivity() {
        Activity activity = null;
        try {
            activity = mStackActivity.lastElement();
        } catch (Exception e) {
        }
        return activity;
    }

    /**
     * 是否是最后一个activity
     * @param activity
     * @return
     */
    public boolean isLastActivity(Activity activity) {
        if (activity != null) {
            return getLastActivity() == activity;
        } else {
            return false;
        }
    }
    /**
     * 是否是最后一个activity
     * @param cls
     * @return
     */
    public boolean isLastActivity(Class<?> cls) {
        if (cls != null) {
            return getLastActivity().getClass() == cls;
        } else {
            return false;
        }
    }

    /**
     * activity是否为空
     * @return
     */
    public boolean isEmpty() {
        return mStackActivity.isEmpty();
    }

    /**
     * 判断activity是否存在
     * @return
     */
    public boolean isExistActivity(Activity activity) {
        Iterator<Activity> it = mStackActivity.iterator();
        while (it.hasNext()) {
            Activity act = it.next();
            if (act == activity) {
                return true;
            }
        }
        return false;
    }
    /**
     * 判断activity是否存在
     * @param cls
     * @return
     */
    public boolean isExistActivity(Class<?> cls) {
        Iterator<Activity> it = mStackActivity.iterator();
        while (it.hasNext()) {
            Activity act = it.next();
            if (act.getClass() == cls) {
                return true;
            }
        }
        return false;
    }
    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        Iterator<Activity> it = mStackActivity.iterator();
        while (it.hasNext()) {
            Activity act = it.next();
            if (act.getClass() == cls) {
                it.remove();
                act.finish();
            }
        }
    }
    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Activity activity) {
        Iterator<Activity> it = mStackActivity.iterator();
        while (it.hasNext()) {
            Activity act = it.next();
            if (act == activity) {
                it.remove();
                act.finish();
            }
        }
    }

    /**
     * 结束所有activity
     */
    public void finishAllActivity() {
        Iterator<Activity> it = mStackActivity.iterator();
        while (it.hasNext()) {
            Activity act = it.next();
            it.remove();
            act.finish();
        }
    }

    /**
     * 结束除指定之外的activity
     * @param cls
     */
    public void finishAllActivityExcept(Class<?> cls) {
        Iterator<Activity> it = mStackActivity.iterator();
        while (it.hasNext()) {
            Activity act = it.next();
            if (act.getClass() != cls) {
                it.remove();
                act.finish();
            }
        }
    }

    /**
     * 结束除指定之外的activity
     * @param activity
     */
    public void finishAllActivityExcept(Activity activity) {
        Iterator<Activity> it = mStackActivity.iterator();
        while (it.hasNext()) {
            Activity act = it.next();
            if (act != activity) {
                it.remove();
                act.finish();
            }
        }
    }
}
